package at.ac.tuwien.mg.service;

import java.util.Vector;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

import at.ac.tuwien.mg.bean.Offer;
import at.ac.tuwien.mg.bean.OfferVector;

public class SaveOffer implements JavaDelegate {

	@Override
	public void execute(DelegateExecution de) throws Exception {
		
		System.out.println("SaveOffer 1");

		OfferVector receivedOffers;
		Vector<Offer> offers;
		
		if(de.hasVariable("receivedOffers")) {
			receivedOffers = (OfferVector) de.getVariable("receivedOffers");
			offers = receivedOffers.getOffers();
		} else {
			receivedOffers = new OfferVector();
			offers = new Vector<Offer>();
		}

		System.out.println("SaveOffer 2");

		if(de.hasVariable("offer")) {
			System.out.println("SaveOffer 4");
			offers.add((Offer) de.getVariable("offer"));
		}
		
		receivedOffers.setOffers(offers);
		de.removeVariable("receivedOffers");
		de.setVariable("receivedOffers", receivedOffers);
		de.setVariable("receivedOffersCount", offers.size());
		
		System.out.println("Displaying received offers: ");
		for(Offer o : offers) {
			System.out.println(o);
		}
		
		System.out.println("SaveOffer 3");
		System.out.println("recievedOffers size: " + receivedOffers.getSize());
	}
}